package prototype.demo.copyPlus;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import prototype.demo.bean.Bean1;
import prototype.demo.bean.Bean2;

import java.io.*;

public class Bean4 implements Serializable {

    private Bean1 bean1;
    private Bean2 bean2;

    public Bean1 getBean1() {
        return bean1;
    }

    public void setBean1(Bean1 bean1) {
        this.bean1 = bean1;
    }

    public Bean2 getBean2() {
        return bean2;
    }

    public void setBean2(Bean2 bean2) {
        this.bean2 = bean2;
    }

    public Bean4(Bean1 bean1, Bean2 bean2){
        this.setBean1(bean1);
        this.setBean2(bean2);
    }

    public Object cloneObject(){
        ByteOutputStream byteOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            byteOutputStream = new ByteOutputStream();
            objectOutputStream = new ObjectOutputStream(byteOutputStream);
            objectOutputStream.writeObject(this);

            byteArrayInputStream = new ByteArrayInputStream(byteOutputStream.getBytes());
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                assert byteOutputStream != null;
                byteOutputStream.close();
                assert objectOutputStream != null;
                objectOutputStream.close();
                assert byteArrayInputStream != null;
                byteArrayInputStream.close();
                assert objectInputStream != null;
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "Bean4{" +
                "bean1=" + bean1 +
                ", bean2=" + bean2 +
                '}';
    }
}
